package com.wgts.wgts.mvp.view.activity.store;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wgts.wgts.ActivityStack;
import com.wgts.wgts.R;
import com.wgts.wgts.base.BaseActivity;
import com.wgts.wgts.control.toast.MoHuToast;
import com.wgts.wgts.dagger.ComponentHolder;
import com.wgts.wgts.dagger.component.DaggerStoreActivityComponent;
import com.wgts.wgts.dagger.module.StoreActivityModule;
import com.wgts.wgts.mvp.presenter.IStoreActivityPresenter;
import com.wgts.wgts.mvp.view.IStoreActivityView;
import com.wgts.wgts.mvp.view.activity.goodsinfo.JDGoodinfoActivity;
import com.wgts.wgts.mvp.view.activity.main.MoHuMainActivity;
import com.wgts.wgts.netmodle.resp.EditStoreEntity;
import com.wgts.wgts.netmodle.resp.ShopKeeperListEntity;
import com.wgts.wgts.netmodle.resp.StoreMsgEntity;
import com.wgts.wgts.tools.CustomImageView;
import com.wgts.wgts.tools.MoHuGlide;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/*
 *
 *
 *   data:2018/10/1118:49
 *
 */public class StoreActivity extends BaseActivity implements IStoreActivityView {


    @Inject
    IStoreActivityPresenter presenter;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.add_goods)
    TextView addGoods;
    @BindView(R.id.my_center_head)
    CircleImageView myCenterHead;
    @BindView(R.id.my_center_name)
    TextView myCenterName;
    @BindView(R.id.my_center_status_text)
    TextView myCenterStatusText;
    @BindView(R.id.my_center_watch_message_ly)
    ImageView myCenterWatchMessageLy;
    @BindView(R.id.store_introduce)
    TextView storeIntroduce;
    @BindView(R.id.my_center_head_ly)
    RelativeLayout myCenterHeadLy;
    @BindView(R.id.store_recycle)
    RecyclerView storeRecycle;
    @BindView(R.id.hiden_back)
    ImageView hidenBack;
    @BindView(R.id.hiden_title_text)
    TextView hidenTitleText;
    @BindView(R.id.show_title)
    TextView showTitle;
    @BindView(R.id.show_back)
    ImageView showBack;
    @BindView(R.id.show_head_line)
    RelativeLayout showHeadLine;
    @BindView(R.id.none_data_ly)
    LinearLayout noneDataLy;
    @BindView(R.id.net_error_ly)
    LinearLayout netErrorLy;
    private List<ShopKeeperListEntity.Product> storeList;
    private StoreAdapter adapter;
    public static final String SHOP_USER_ID = "userId";
    public static final String USER_TYPE = "userType";
    public static final String SHOP_ID = "shopId";
    private String userId;
    private String userType;
    private Integer shopId;
    private StoreMsgEntity storeMsgEntity;
    private EditStoreEntity editStoreEntity;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.store_activity);
        ButterKnife.bind(this);
        Intent intent = getIntent();
        userType = intent.getStringExtra(USER_TYPE);
        userId = intent.getStringExtra(SHOP_USER_ID);
        if (intent.getStringExtra(SHOP_ID) != null && !intent.getStringExtra(SHOP_ID).equals("")) {
            shopId = Integer.parseInt(intent.getStringExtra(SHOP_ID));
        }
        CustomImageView.getInstance().customView(this, myCenterHeadLy, 432 / (double) 750);
        if (userType != null) {
            myCenterWatchMessageLy.setClickable(true);
            myCenterWatchMessageLy.setVisibility(View.VISIBLE);
            addGoods.setVisibility(View.VISIBLE);
            addGoods.setClickable(true);
        } else {
            myCenterWatchMessageLy.setClickable(false);
            myCenterWatchMessageLy.setVisibility(View.GONE);
            addGoods.setVisibility(View.GONE);
            addGoods.setClickable(false);
        }
        initView();
    }

    @Override
    public void init() {
        DaggerStoreActivityComponent
                .builder()
                .appComponent(ComponentHolder.getInstance().getAppComponent())
                .storeActivityModule(new StoreActivityModule(this))
                .build()
                .inject(this);
        storeList = new ArrayList<>();
    }

    private void initView() {
        refreshLayout.setEnableRefresh(false);
        refreshLayout.setEnableLoadmore(true);
        storeRecycle.setLayoutManager(new LinearLayoutManager(this));
        adapter = new StoreAdapter(this, storeList, userType);
        storeRecycle.setAdapter(adapter);
        presenter.initList(shopId);
        load();
        presenter.storeMsg(shopId);

        adapter.setOnClickListener(new StoreAdapter.OnClickListener() {
            @Override
            public void onClick(View view, int i) {
                Intent intent = new Intent(StoreActivity.this, JDGoodinfoActivity.class);
                intent.putExtra(JDGoodinfoActivity.JOIN_TYPE, JDGoodinfoActivity.NOMAL);
                intent.putExtra(JDGoodinfoActivity.PRODUCT_ID, String.valueOf(storeList.get(i).productId));
                startActivity(intent);
            }
        });

        adapter.setCancelClickListen(new StoreAdapter.cancelClickListen() {
            @Override
            public void cancelClick(View view, int position) {
                presenter.upDownProduct(String.valueOf(storeList.get(position).productId), position);
            }
        });
    }

    @OnClick({R.id.my_center_watch_message_ly, R.id.add_goods, R.id.show_back, R.id.hiden_back, R.id.net_error_ly})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.show_back:
                finish();
                break;
            case R.id.hiden_back:
                finish();
                break;
            case R.id.my_center_watch_message_ly:
                if (storeMsgEntity != null) {
                    Intent intent = new Intent(StoreActivity.this, EditStoreActivity.class);
                    intent.putExtra(EditStoreActivity.STORE_MSG, storeMsgEntity);
                    startActivityForResult(intent, EditStoreActivity.EDIT_STORE_RESULT_OK);
                }
                break;
            case R.id.add_goods:
                for (BaseActivity activity : ActivityStack.getInstance().getActivities()) {
                    if (activity != null && activity instanceof MoHuMainActivity) {
                        ((MoHuMainActivity) activity).changeViewPage(0);
                    }
                }
                finish();
                break;
            case R.id.net_error_ly:
                presenter.initList(shopId);
                presenter.storeMsg(shopId);
                break;
        }
    }

    @Override
    public void onError(int status, String msg) {
        noneDataLy.setVisibility(View.GONE);
        netErrorLy.setVisibility(View.VISIBLE);
        MoHuToast.showToast(msg);
    }

    @Override
    public void initList(ShopKeeperListEntity entity, boolean noMore) {
        if (entity.list.size() > 0){
            noneDataLy.setVisibility(View.GONE);
            netErrorLy.setVisibility(View.GONE);
        }else {
            noneDataLy.setVisibility(View.VISIBLE);
            netErrorLy.setVisibility(View.GONE);
        }
        adapter.setNoMore(noMore);
        storeList.clear();
        storeList.addAll(entity.list);
        adapter.notifyDataSetChanged();
        if (noMore) {
            refreshLayout.setEnableLoadmore(false);
        } else {
            refreshLayout.setEnableLoadmore(true);
        }
    }

    @Override
    public void loadMore(final ShopKeeperListEntity entity, boolean noMore) {
        refreshLayout.finishLoadmore();
        adapter.setNoMore(noMore);
        storeList.addAll(entity.list);
        adapter.notifyDataSetChanged();
        if (noMore) {
            refreshLayout.setEnableLoadmore(false);
        } else {
            refreshLayout.setEnableLoadmore(true);
        }


    }

    @Override
    public void storeMsg(StoreMsgEntity msgEntity) {
        netErrorLy.setVisibility(View.GONE);
        MoHuGlide.getInstance().glideImg(this, msgEntity.shopImage, myCenterHead);
        if (userType != null) {
            showTitle.setText("我的店铺");
            hidenTitleText.setText("我的店铺");
        } else {
            showTitle.setText(msgEntity.shopName);
            hidenTitleText.setText(msgEntity.shopName);
        }
        myCenterName.setText(msgEntity.shopName);
        if (msgEntity.shopDesc != null && !msgEntity.shopDesc.equals("")) {
            storeIntroduce.setText("简介：" + msgEntity.shopDesc);
        } else {
            storeIntroduce.setText("店家很懒，什么都没留下...");
        }
        myCenterStatusText.setText("店铺ID：" + msgEntity.shopCode);
        storeMsgEntity = msgEntity;
    }

    @Override
    public void upDownProductResult(int position) {
        MoHuToast.showToast("下架成功");
        storeList.remove(position);
        adapter.notifyDataSetChanged();
        if (storeList.size() < 1){
            netErrorLy.setVisibility(View.GONE);
            noneDataLy.setVisibility(View.VISIBLE);
        }
    }

    private void load() {
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                presenter.loadMore(shopId);
            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == EditStoreActivity.EDIT_STORE_RESULT_OK && data != null) {
            //进行个人信息刷新
            presenter.storeMsg(shopId);
        }
    }

}
